package org.lex.swing;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class WindowBoundsDemo extends javax.swing.JFrame {
	private JLabel locationLabel;
	private JLabel sizeLabel;
	private JPanel gluePane;
	private JTextField sizeTextField;
	private JTextField locationTextField;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				WindowBoundsDemo inst = new WindowBoundsDemo();
				inst.setLocationByPlatform(true);
				inst.setVisible(true);
			}
		});
	}

	public WindowBoundsDemo() {
		super();
		initGUI();
		this.addComponentListener(new ComponentListener() {
			public void componentMoved(ComponentEvent e) {
				Point loc = getLocation();
				locationTextField.setText(loc.x + ", " + loc.y);
			}

			public void componentResized(ComponentEvent e) {
				Dimension size = getSize();
				sizeTextField.setText(size.width + ", " + size.height);
			}

			public void componentShown(ComponentEvent e) {
			}

			public void componentHidden(ComponentEvent e) {
			}
		});
	}

	private void initGUI() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		GridBagLayout thisLayout = new GridBagLayout();
		this.setTitle("Window Size Demo");
		thisLayout.rowWeights = new double[] { 0.1, 0.1, 0.1 };
		thisLayout.rowHeights = new int[] { 7, 7, 7 };
		thisLayout.columnWeights = new double[] { 0.1, 0.1 };
		thisLayout.columnWidths = new int[] { 7, 7 };
		getContentPane().setLayout(thisLayout);
		getContentPane().add(getLocationLabel(),
				new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(getLocationTextField(),
				new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(getSizeLabel(),
				new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(getSizeTextField(),
				new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(getGluePane(),
				new GridBagConstraints(0, 2, 2, 1, 10.0, 10.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		setSize(400, 300);
	}

	private JLabel getLocationLabel() {
		if (locationLabel == null) {
			locationLabel = new JLabel();
			locationLabel.setText("Window Location:");
		}
		return locationLabel;
	}

	private JTextField getLocationTextField() {
		if (locationTextField == null) {
			locationTextField = new JTextField(10);
			locationTextField.setText("0, 0");
			locationTextField.setEditable(false);
		}
		return locationTextField;
	}

	private JLabel getSizeLabel() {
		if (sizeLabel == null) {
			sizeLabel = new JLabel();
			sizeLabel.setText("Window Size:");
		}
		return sizeLabel;
	}

	private JTextField getSizeTextField() {
		if (sizeTextField == null) {
			sizeTextField = new JTextField(10);
			sizeTextField.setText("100, 200");
			sizeTextField.setEditable(false);
		}
		return sizeTextField;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
		}
		return gluePane;
	}
}
